package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.Address;
import com.qianniu.napi.admin.service.IAddressService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * 收货地址
 * </p>
 *
 * @author chenxuebin
 * @since 2018-06-11
 */
@Controller
public class AddressController {

    private static final Logger logger = LoggerFactory.getLogger(AddressController.class);

    @Autowired
    private IAddressService addressService;

    @RequiresPermissions("admin:user:view")
    @RequestMapping("/admin/address/index")
    public String index() {
        return "admin/address";
    }

    @RequiresPermissions("admin:user:view")
    @RequestMapping("/admin/address/list")
    @ResponseBody
    public Page<Address> alist(@RequestParam(value = "offset", required = false) Integer offset,
                               @RequestParam(value = "status", required = false) Integer status,
                               @RequestParam(value = "search", required = false) String search,
                               @RequestParam(value = "limit", required = false) Integer limit) {

        Wrapper<Address> wrapper = new EntityWrapper<Address>();
        int utype = ObjectUtil.getUserType();
        if (utype < 39)
            wrapper.eq("mpid", ObjectUtil.getMpid());
        else if (utype < 99)
            wrapper.eq("mid", ObjectUtil.getMid());

//        if (status!=null&&status != 99) wrapper.eq("gtype", status);
        if (!Strings.isNullOrEmpty(search))
            wrapper.like("address", search);

        wrapper.orderBy("id", false);
        Page<Address> page = addressService.selectPage(new Page<Address>(offset / limit+1, limit), wrapper);
//        Page<Address> page = addressService.selectPage(new Page<Address>(0, 10));
        return page;
    }

    @RequestMapping("/admin/address/add")
    public String add(@Valid Address bean, BindingResult bindingResult) {
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            return null;
        }
        bean.setCreatetime(new Date());
        bean.setMid(ObjectUtil.getMid());
        bean.setUid(ObjectUtil.getUid());
        bean.insert();
        return "admin/address";
    }


    @RequestMapping("/admin/address/getAddressById")
    @ResponseBody
    public Address getAddressById(long id) {
        Address bean = addressService.selectById(id);
        return bean;
    }

    @RequestMapping("/admin/address/deleteOne")
    @ResponseBody
    public int deleteOne(long id) {
        boolean isSucc = addressService.deleteById(id);
        return isSucc ? 1 : 0;
    }

    @RequestMapping("/admin/address/changeState")
    @ResponseBody
    public String changeState(long id, int state) {
        Address bean = new Address();
        bean.setId(id);
        bean.setState(state);
        bean.setModifytime(new Date());
        bean.setModifier(ObjectUtil.getUid());
        addressService.updateById(bean);
        return "success";
    }

    @RequestMapping("/admin/address/modify")
    public String update(Address bean) {
        bean.setModifytime(new Date());
        bean.setModifier(ObjectUtil.getUid());
        boolean isSuccc = addressService.updateById(bean);
        if (isSuccc) {
            return "redirect:index";
        }
        return "error";
    }

//    @RequestMapping("/address/list")
//    @ResponseBody
//    public Page<Address> list(@RequestParam(value = "ptype", required = false) String ptype) {
//        if (Strings.isNullOrEmpty(ptype)) {
//            ptype = "1";
//        }
//        Page<Address> page = new Page<Address>(0, 6);
//        Wrapper wrapper = new EntityWrapper<Address>().eq("state", "1").eq("ptype", ptype);
//        Page<Address> list = addressService.selectPage(page, wrapper);
//        return list;
//    }

}
